
package cn.dakaqi.utils.sms.sms189;

import java.net.URLEncoder;
import java.util.Random;
import java.util.TreeMap;

import cn.dakaqi.utils.DateUtil;
import open189.sign.ParamsSign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendSMS
{
	//{"res_code":0,"identifier":"ZN2509","create_at":1399190667}
	static Logger log = LoggerFactory.getLogger(SendSMS.class);
	/**
	 * 电信系统验证码下发
	 * @param userPhone
	 * @return
	 * @throws Exception
	 */
	public static String sendMsg9(String userPhone)throws Exception
	{
		String sendSMSBasePath = "http://api.189.cn/v2/dm/randcode/send";
		String access_token = AccesssToken.getAccessToken();
		String token = Token.getToken(access_token);
		
		String timestamp = DateUtil.getToday();
		TreeMap<String, String> paramsMap1 = new TreeMap<String, String>();
		paramsMap1.put("app_id", SMS.app_id);
		paramsMap1.put("access_token", access_token);
		paramsMap1.put("timestamp", timestamp);
		paramsMap1.put("token", token);
		paramsMap1.put("url", SMS.redirect_uri);
		paramsMap1.put("phone", userPhone);
		paramsMap1.put("exp_time", "2");
		
		String postUrl = sendSMSBasePath + "?app_id="
		                 + SMS.app_id + "&access_token=" + access_token + "&timestamp=" + URLEncoder.encode(timestamp, "UTF-8") + "";
		String postEntity = "token=" + token
				          + "&phone=" + userPhone
				          + "&url=" + SMS.redirect_uri// 有开发者实现的验证码通知接口
				          + "&exp_time=" + "2"
				          + "&sign="+ParamsSign.value(paramsMap1, SMS.app_secret);
		
		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}
	
	//{"res_code":0,"identifier":"ZN2509","create_at":1399190667}

	
	//{"res_code":"0","res_message":"Success","idertifier":"90610519115436051079"}
	/**
	 * 手机验证码消息
	 * @param userPhone
	 * @param randCode
	 * @return
	 * @throws Exception
	 */
	public static String sendMsgFromTemplate(String userPhone,String randCode)throws Exception
	{
		String access_token = AccesssToken.getAccessToken();
		String para = "{\"code\":\""+randCode+"\",\"time\":\"10\"}";
		String timestamp = DateUtil.getToday();
		
		String postUrl = "http://api.189.cn/v2/emp/templateSms/sendSms";
		String postEntity = "app_id=" + SMS.app_id
        + "&acceptor_tel=" + userPhone
        + "&access_token=" + access_token
        + "&template_id=91000900"
        + "&template_param=" + para
        + "&timestamp=" + timestamp;
		
		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}
	/**
	 * 超级管理员审核通过消息
	 * @param userPhone
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	public static String sendNotificationFromTemplate(String userPhone,String msg)throws Exception
	{
		String access_token = AccesssToken.getAccessToken();
		String para = "{\"account\":\""+URLEncoder.encode(msg, "UTF-8")+"\"}";
		String timestamp = DateUtil.getToday();
		
		String postUrl = "http://api.189.cn/v2/emp/templateSms/sendSms";
		String postEntity = "app_id=" + SMS.app_id
        + "&acceptor_tel=" + userPhone
        + "&access_token=" + access_token
        + "&template_id=91001398"
        + "&template_param=" + para
        + "&timestamp=" + timestamp;
		
		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}
	/**
	 * 超级管理员审核拒绝消息
	 * @param userPhone
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	public static String sendRefuseNotificationFromTemplate(String userPhone,String msg)throws Exception
	{
		String access_token = AccesssToken.getAccessToken();
		String para = "{\"cause\":\""+URLEncoder.encode(msg, "UTF-8")+"\"}";
		String timestamp = DateUtil.getToday();
		
		String postUrl = "http://api.189.cn/v2/emp/templateSms/sendSms";
		String postEntity = "app_id=" + SMS.app_id
        + "&acceptor_tel=" + userPhone
        + "&access_token=" + access_token
        + "&template_id=91001448"
        + "&template_param=" + para
        + "&timestamp=" + timestamp;
		
		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}
	//主管单位审核通过
	public static String sendDepartmentNotificationFromTemplate(String userPhone,String deparmentName,String account)throws Exception
	{
		String access_token = AccesssToken.getAccessToken();
		//String para = "{\"code\":\""+randCode+"\",\"time\":\"10\"}";
		String para = "{\"account\":\""+account+"\",\"department\":\""+URLEncoder.encode(deparmentName, "UTF-8")+"\"}";
		String timestamp = DateUtil.getToday();

		String postUrl = "http://api.189.cn/v2/emp/templateSms/sendSms";
		String postEntity = "app_id=" + SMS.app_id
				+ "&acceptor_tel=" + userPhone
				+ "&access_token=" + access_token
				+ "&template_id=91003908"
				+ "&template_param=" + para
				+ "&timestamp=" + timestamp;

		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}
	//主管单位审核拒绝
	public static String sendRefuseDepartmentNotificationFromTemplate(String userPhone,String deparmentName,String msg)throws Exception
	{
		String access_token = AccesssToken.getAccessToken();
		String para = "{\"case\":\""+URLEncoder.encode(msg, "UTF-8")+"\",\"deparment\":\""+URLEncoder.encode(deparmentName, "UTF-8")+"\"}";
		//String para = "{\"cause\":\""+URLEncoder.encode(msg, "UTF-8")+"\"}";
		String timestamp = DateUtil.getToday();

		String postUrl = "http://api.189.cn/v2/emp/templateSms/sendSms";
		String postEntity = "app_id=" + SMS.app_id
				+ "&acceptor_tel=" + userPhone
				+ "&access_token=" + access_token
				+ "&template_id=91003907"
				+ "&template_param=" + para
				+ "&timestamp=" + timestamp;

		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}

	//{"res_code":"0","res_message":"设置成功"}
	private static String setAppUrl(String url)throws Exception
	{
		String postUrl = "http://api.189.cn/v2/EMP/nsagSms/appnotify/setappurl";
		String access_token = AccesssToken.getAccessToken();
		
		String timestamp = DateUtil.getToday();
		TreeMap<String, String> paramsMap1 = new TreeMap<String, String>();
		paramsMap1.put("app_id", SMS.app_id);
		paramsMap1.put("access_token", access_token);
		paramsMap1.put("timestamp", timestamp);
		paramsMap1.put("api_url", url);
		paramsMap1.put("api_type", "2");
		
		
		postUrl = postUrl + "?app_id="
		                 + SMS.app_id + "&access_token=" + access_token + "&timestamp=" + URLEncoder.encode(timestamp, "UTF-8") + "";
		String postEntity = "api_type=2"
				          + "&api_url=" + url
				          + "&sign="+ParamsSign.value(paramsMap1, SMS.app_secret);
		
		return HttpInvoker.httpPost(postUrl,null,postEntity);
		
//		app_id	true	string	应用ID，开发者创建应用时，天翼开放平台会为应用自动分配此ID，开发者进入"管理中心"，在“应用详情”中可查看app_id。
//		access_token	true	string	访问令牌，是调用平台能力接口的通行证，可通过调用"令牌接口"获得。
//		api_type	true	string	接口类型，1表示上行消息接收地址（暂不支持），2表示状态报告接收接口地址。
//		api_url	true	string	消息接收地址。
//		timestamp	true	string	时间戳，格式为：yyyy-MM-dd HH:mm:ss
		
		
	}
	//{"res_code":"0","identifier":"90610519115436051079","status ":"1","res_message":""}
	private static String querySmsStatus(String identifier)throws Exception
	{
		
//		app_id	true	string	应用ID，开发者创建应用时，天翼开放平台会为应用自动分配此ID，开发者进入"管理中心"，在“应用详情”中可查看app_id。
//		access_token	true	string	访问令牌，是调用平台能力接口的通行证，可通过调用"令牌接口"获得。
//		identifier	true	string	短信唯一标识，成功调用“发送模板短信”接口时返回。
//		timestamp	true	string	时间
		
		String postUrl = "http://api.189.cn/v2/EMP/nsagSms/appnotify/querysmsstatus";
		String access_token = AccesssToken.getAccessToken();
		
		String timestamp = DateUtil.getToday();
		TreeMap<String, String> paramsMap1 = new TreeMap<String, String>();
		paramsMap1.put("app_id", SMS.app_id);
		paramsMap1.put("access_token", access_token);
		paramsMap1.put("timestamp", timestamp);
		paramsMap1.put("identifier", identifier);
		
		
		postUrl = postUrl + "?app_id="
		                 + SMS.app_id + "&access_token=" + access_token + "&timestamp=" + URLEncoder.encode(timestamp, "UTF-8") + "";
		String postEntity = "identifier=" + identifier
				          + "&sign="+ParamsSign.value(paramsMap1, SMS.app_secret);
		
		return HttpInvoker.httpPost(postUrl,null,postEntity);
	}
	private static int get4Rand()
	{
		Random rand = new Random(); 
	    int tmp = Math.abs(rand.nextInt());
	    return tmp % (9999 - 1000 + 1) + 1000;
	}
	private static int get6Rand()
	{
		Random rand = new Random(); 
	    int tmp = Math.abs(rand.nextInt());
	    return tmp % (999999 - 100000 + 1) + 100000;
	}
	public static void main(String[] args)
	{
		try
		{
			//系统发验证码
			//System.out.println(sendMsg("15821117931"));
			//自定义验证码
			//System.out.println(sendMsgFromTemplate2("18939731511",String.valueOf(getRand())));
			//System.out.println(sendMsgFromTemplate9("15821117931", "1234"));
			//模板发
			System.out.println(sendMsgFromTemplate("15821117931",String.valueOf(get4Rand())));
			//审核通过
			//System.out.println(sendNotificationFromTemplate("18916666237","感谢您一年的志愿服务"));
			//审核拒绝
			//System.out.println(sendRefuseNotificationFromTemplate("18939731511","  感谢您一年的志愿服务，南通团市委给您准备了专属福利，赶紧去志愿者打卡器看看吧。"));
			//System.out.println(sendDepartmentNotificationFromTemplate("17091266898","共青团南通市委","nt001"));
			//System.out.println(sendRefuseDepartmentNotificationFromTemplate("17091266898","共青团南通市委","不是本单位成员"));
			//模板短信消息接收地址设置
			//System.out.println(setAppUrl("http://www.dakaqi.cn/sms/receive-sms.action"));
			//模板短信状态报告查询
			//System.out.println(querySmsStatus("90610519115436051079"));
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();log.error(e.getMessage());

		}
	}
	
}
